#include <cstdlib>
#include <fstream>
#include <iostream>

using std::cout;
using std::endl;
using std::ifstream;
using std::string;

int main()
{
    string filename("1_io.cc");
    ifstream ifs(filename);
    //if(!ifs.good())  与其效果相同
    if(!ifs) {
        cout << "ifstream open file error" << endl;
        return EXIT_FAILURE;
    }

    //获取文件游标的定位信息
    int pos = ifs.tellg();
    cout << "pos: " << pos << endl;
    //偏移到文件流的末尾
    ifs.seekg(0, std::ios::end);
    pos = ifs.tellg();
    cout << "pos: " << pos << endl;
    //char buff[10000000];
    //偏移到文件流的开始
    ifs.seekg(0);
    //当pos的值不确定的情况下，应该将空间开在堆上
    char * pbuff = new char[pos + 1]();
    ifs.read(pbuff, pos);
    string file(pbuff);//将C风格的字符串转为C++风格的字符串
    cout << "file:" << file << endl;

    delete [] pbuff;
    pbuff = nullptr;

    return 0;
}

